package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.News;
import com.example.entity.Rank;
import com.example.mapper.NewsMapper;
import com.example.mapper.RankMapper;
import com.example.qo.NewsQuery;
import com.example.qo.RankQuery;
import com.example.service.INewsService;
import com.example.service.IRankService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xs
 * @since 2023-04-14
 */
@Service
public class RankServiceImpl extends ServiceImpl<RankMapper, Rank> implements IRankService {

    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private INewsService newsService;

    @Override
    public Page<News> queryPage(RankQuery qo, NewsQuery nqo) {
        Page<News> pageNews = newsService.queryPage(nqo);
        List<News> news = pageNews.getRecords();
        for (News n : news) {
            int days = (int) (0 - LocalDate.now().until(n.getCreateTime(), ChronoUnit.DAYS));

            if (days == 0) {
                n.setCount(n.getCount());
            } else if (days == 1) {
                n.setCount(n.getViewnum() * 0.9);
            } else if (days == 2) {
                n.setCount(n.getViewnum() * 0.8);
            } else if (days == 3) {
                n.setCount(n.getViewnum() * 0.7);
            } else if (days == 4) {
                n.setCount(n.getViewnum() * 0.6);
            } else if (days == 5) {
                n.setCount(n.getViewnum() * 0.5);
            } else if (days == 6) {
                n.setCount(n.getViewnum() * 0.4);
            } else if (days >= 7) {
                n.setCount(0.0);
            }
        }

        Page<News> page = new Page<>();
        page.setCurrent(qo.getCurrent());
        page.setSize(qo.getSize());

        QueryWrapper<News> qw= new QueryWrapper<>();
        qw.orderByDesc("count");
        qw.last("limit 5");

        return newsMapper.selectPage(page,qw);
    }
}
